#pragma once
#include "uart/ProtocolSender.h"
#include "logic/global_variables.h"
#include "water/timer_event.h"
#include "logic_utility.hpp"








//设置列表自定义时间
static void ListTime(){
		 mhourList0Ptr->setSelection(Global::Timer::coldHL == 0?23:Global::Timer::coldHL-1);
		 mminList0Ptr->setSelection(Global::Timer::coldML ==0? 59:Global::Timer::coldML-1);
		 mhourList1Ptr->setSelection(Global::Timer::coldHR == 0?23:Global::Timer::coldHR-1);
		 mminList1Ptr->setSelection(Global::Timer::coldMR ==0? 59:Global::Timer::coldMR-1);

		char buf [36]={0};
		snprintf(buf,sizeof(buf),"%02d:%02d-%02d:%02d",Global::Timer::coldHL, Global::Timer::coldML,
				Global::Timer::coldHR, Global::Timer::coldMR);
		 mTextTimePtr->setText(buf );
}
/**
 * 注册定时器
 * 填充数组用于注册定时器
 * 注意：id不能重复
 */
static S_ACTIVITY_TIMEER REGISTER_ACTIVITY_TIMER_TAB[] = {
	//{0,  6000}, //定时器id=0, 时间间隔6秒
	{1,  300},
};

/**
 * 当界面构造时触发
 */
static void onUI_init(){
    //Tips :添加 UI初始化的显示代码到这里,如:mText1Ptr->setText("123");
	mcoldSwitchPtr->setSelected(isColdOpen());
	ListTime();
	if(Global::Flags::custom_cold){
		mcustomSwitchPtr->setSelected(true);
	}else{
		mallDayPtr->setSelected(true);
	    mButtonDPtr->setInvalid(true);
	}
	mcoldBarPtr->setProgress(Global::Water::coldTemp - 4);
}

/**
 * 当切换到该界面时触发
 */
static void onUI_intent(const Intent *intentPtr) {
    if (intentPtr != NULL) {
        //TODO
    }
}

/*
 * 当界面显示时触发
 */
static void onUI_show() {

}

/*
 * 当界面隐藏时触发
 */
static void onUI_hide() {

}

/*
 * 当界面完全退出时触发
 */
static void onUI_quit() {

}

/**
 * 串口数据回调接口
 */
static void onProtocolDataUpdate(const SProtocolData &data) {

}

/**
 * 定时器触发函数
 * 不建议在此函数中写耗时操作，否则将影响UI刷新
 * 参数： id
 *         当前所触发定时器的id，与注册时的id相同
 * 返回值: true
 *             继续运行当前定时器
 *         false
 *             停止运行当前定时器
 */
static bool onUI_Timer(int id){
	switch (id) {
	case 1:{
		mcoldSwitchPtr->setSelected(isColdOpen());
	}break;
		default:
			break;
	}
    return true;
}

/**
 * 有新的触摸事件时触发
 * 参数：ev
 *         新的触摸事件
 * 返回值：true
 *            表示该触摸事件在此被拦截，系统不再将此触摸事件传递到控件上
 *         false
 *            触摸事件将继续传递到控件上
 */
static bool oncoldWaterActivityTouchEvent(const MotionEvent &ev) {
    switch (ev.mActionStatus) {
		case MotionEvent::E_ACTION_DOWN://触摸按下
			//LOGD("时刻 = %ld 坐标  x = %d, y = %d", ev.mEventTime, ev.mX, ev.mY);
			break;
		case MotionEvent::E_ACTION_MOVE://触摸滑动
			break;
		case MotionEvent::E_ACTION_UP:  //触摸抬起
			break;
		default:
			break;
	}
	return false;
}


static bool onButtonClick_allDay(ZKButton *pButton) {
    LOGD(" ButtonClick allDay !!!\n");
    pButton->setSelected(true);
    mcustomSwitchPtr->setSelected(false);
    mButtonDPtr->setInvalid(true);
    return false;
}

static bool onButtonClick_ButtonD(ZKButton *pButton) {
    LOGD(" ButtonClick ButtonD !!!\n");
    mtimeWndPtr->showWnd();
    return false;
}

static bool onButtonClick_customSwitch(ZKButton *pButton) {
    LOGD(" ButtonClick customSwitch !!!\n");
    mallDayPtr->setSelected(false);
    pButton->setSelected(true);
    mButtonDPtr->setInvalid(false);
    return false;
}

static bool onButtonClick_sys_back(ZKButton *pButton) {
    LOGD(" ButtonClick sys_back !!!\n");
    return false;
}
static bool onButtonClick_coldSwitch(ZKButton *pButton) {
    LOGD(" ButtonClick coldSwitch !!!\n");
    operateCold(!isColdOpen());
    mcoldSwitchPtr->setSelected(isColdOpen());
    return false;
}

static void onProgressChanged_coldBar(ZKSeekBar *pSeekBar, int progress) {
    //LOGD(" ProgressChanged coldBar %d !!!\n", progress);
    mcoldTempPtr->setText(to_string(progress + 4) + "℃");
}


static bool onButtonClick_timeOk(ZKButton *pButton) {
    LOGD(" ButtonClick timeOk !!!\n");
    Global::Timer::coldHL = ( (mhourList0Ptr->getFirstVisibleItemIndex()+1)% mhourList0Ptr->getListItemCount() );
    Global::Timer::coldHR = ( (mhourList1Ptr->getFirstVisibleItemIndex()+1)% mhourList1Ptr->getListItemCount() );
    Global::Timer::coldML = ( (mminList0Ptr->getFirstVisibleItemIndex()+1)% mminList0Ptr->getListItemCount() );
    Global::Timer::coldMR = ( (mminList1Ptr->getFirstVisibleItemIndex()+1)% mminList1Ptr->getListItemCount() );
    ListTime();
    mtimeWndPtr->hideWnd();
    return false;
}

static bool onButtonClick_timeCancel(ZKButton *pButton) {
    LOGD(" ButtonClick timeCancel !!!\n");
    mtimeWndPtr->hideWnd();
    return false;
}

static int getListItemCount_hourList0(const ZKListView *pListView) {
    //LOGD("getListItemCount_hourList0 !\n");
    return 24;
}

static void obtainListItemData_hourList0(ZKListView *pListView,ZKListView::ZKListItem *pListItem, int index) {
    //LOGD(" obtainListItemData_ hourList0  !!!\n");
	char buf [10]={0};
	snprintf(buf,sizeof(buf),"%02d时",index);
	pListItem->setText(buf);
	pListItem->setTextColor(index == (pListView->getFirstVisibleItemIndex()+1)%pListView->getListItemCount()?  0xFFFFFF : 0x474D50);
	pListItem->setTextSize(index == (pListView->getFirstVisibleItemIndex()+1)%pListView->getListItemCount()? 34 : 28);

}

static void onListItemClick_hourList0(ZKListView *pListView, int index, int id) {
    //LOGD(" onListItemClick_ hourList0  !!!\n");
}

static int getListItemCount_minList0(const ZKListView *pListView) {
    //LOGD("getListItemCount_minList0 !\n");
    return 60;
}

static void obtainListItemData_minList0(ZKListView *pListView,ZKListView::ZKListItem *pListItem, int index) {
    //LOGD(" obtainListItemData_ minList0  !!!\n");
	char buf [10]={0};
	snprintf(buf,sizeof(buf),"%02d分",index);
	pListItem->setText(buf);
	pListItem->setTextColor(index == (pListView->getFirstVisibleItemIndex()+1)%pListView->getListItemCount()?  0xFFFFFF : 0x474D50);
	pListItem->setTextSize(index == (pListView->getFirstVisibleItemIndex()+1)%pListView->getListItemCount()? 34 : 28);
}

static void onListItemClick_minList0(ZKListView *pListView, int index, int id) {
    //LOGD(" onListItemClick_ minList0  !!!\n");
}

static int getListItemCount_hourList1(const ZKListView *pListView) {
    //LOGD("getListItemCount_hourList1 !\n");
    return 24;
}

static void obtainListItemData_hourList1(ZKListView *pListView,ZKListView::ZKListItem *pListItem, int index) {
    //LOGD(" obtainListItemData_ hourList1  !!!\n");
	char buf [10]={0};
	snprintf(buf,sizeof(buf),"%02d时",index);
	pListItem->setText(buf);
	pListItem->setTextColor(index == (pListView->getFirstVisibleItemIndex()+1)%pListView->getListItemCount()?  0xFFFFFF : 0x474D50);
	pListItem->setTextSize(index == (pListView->getFirstVisibleItemIndex()+1)%pListView->getListItemCount()? 34 : 28);
}

static void onListItemClick_hourList1(ZKListView *pListView, int index, int id) {
    //LOGD(" onListItemClick_ hourList1  !!!\n");
}

static int getListItemCount_minList1(const ZKListView *pListView) {
    //LOGD("getListItemCount_minList1 !\n");
    return 60;
}

static void obtainListItemData_minList1(ZKListView *pListView,ZKListView::ZKListItem *pListItem, int index) {
    //LOGD(" obtainListItemData_ minList1  !!!\n");
	char buf [10]={0};
	snprintf(buf,sizeof(buf),"%02d分",index);
	pListItem->setText(buf);
	pListItem->setTextColor(index == (pListView->getFirstVisibleItemIndex()+1)%pListView->getListItemCount()?  0xFFFFFF : 0x474D50);
	pListItem->setTextSize(index == (pListView->getFirstVisibleItemIndex()+1)%pListView->getListItemCount()? 34 : 28);
}

static void onListItemClick_minList1(ZKListView *pListView, int index, int id) {
    //LOGD(" onListItemClick_ minList1  !!!\n");
}

static bool onButtonClick_ok(ZKButton *pButton) {
    LOGD(" ButtonClick ok !!!\n");
	if(mcustomSwitchPtr->isSelected()){
		Global::Flags::custom_cold = true;
		Global::j_handler.saveInt("startcoldTH", Global::Timer::coldHL);
		Global::j_handler.saveInt("startcoldTM", Global::Timer::coldML);

		Global::j_handler.saveInt("endcoldTH", Global::Timer::coldHR);
		Global::j_handler.saveInt("endcoldTM", Global::Timer::coldMR);
	}else{
		Global::Flags::custom_cold = false;
	}
	Global::j_handler.saveBool("customcold", Global::Flags::custom_cold);
	Global::Water::coldTemp = (mcoldBarPtr->getProgress() + 4 );
	Global::j_handler.saveInt("coldTemp", Global::Water::coldTemp);
    EASYUICONTEXT->goBack();
    return false;
}

static bool onButtonClick_cancel(ZKButton *pButton) {
    LOGD(" ButtonClick cancel !!!\n");
    EASYUICONTEXT->goBack();
    return false;
}
